package cn.trasen.his.poc.std.core.action;

import cn.trasen.his.poc.std.api.emums.BedStatusEnum;
import cn.trasen.his.poc.std.api.emums.CardStatusEnum;
import cn.trasen.his.poc.std.api.emums.PatBedCardStatusEnum;
import cn.trasen.his.poc.std.api.emums.PersonClassEnum;
import cn.trasen.his.poc.std.api.model.designer.DesignerModelView;
import cn.trasen.his.poc.std.api.pmodel.WardBedCardProxy;
import org.springframework.stereotype.Component;
import pro.shushi.pamirs.meta.annotation.Action;
import pro.shushi.pamirs.meta.annotation.Function;
import pro.shushi.pamirs.meta.annotation.Model;
import pro.shushi.pamirs.meta.constant.ExpConstants;
import pro.shushi.pamirs.meta.constant.FunctionConstants;
import pro.shushi.pamirs.meta.enmu.FunctionOpenEnum;
import pro.shushi.pamirs.meta.enmu.FunctionTypeEnum;
import pro.shushi.pamirs.meta.enmu.ViewTypeEnum;

@Component
@Model.model(DesignerModelView.MODEL_MODEL)
public class DesignerModelViewAction {

    @Function(openLevel = FunctionOpenEnum.API)
    @Function.Advanced(type = FunctionTypeEnum.QUERY)
    public DesignerModelView construct(DesignerModelView data) {
        data.setModel(WardBedCardProxy.MODEL_MODEL);
        data.setCardStatus(CardStatusEnum.ALL);
        data.setPersonClass(PersonClassEnum.ADULT);
        data.setPatBedCardStatus(PatBedCardStatusEnum.IN_HOSP);
        data.setBedStatus(BedStatusEnum.EMPTYBED);

        DesignerModelView dbModelView = new DesignerModelView()
                .setCardStatus(CardStatusEnum.ALL)
                .setPersonClass(PersonClassEnum.ADULT)
                .setPatBedCardStatus(PatBedCardStatusEnum.IN_HOSP)
                .setBedStatus(BedStatusEnum.EMPTYBED).queryOne();
        if (dbModelView != null) {
            data.setId(dbModelView.getId());
            data.setTemplateJson(dbModelView.getTemplateJson());
        }
        return data;
    }

    @Function(openLevel = FunctionOpenEnum.API, summary = "选择床位状态后联动")
    @Function.Advanced(displayName = "选择床位状态后联动", type = FunctionTypeEnum.QUERY)
    public DesignerModelView constructBedCardStatusChg(DesignerModelView data) {
        if (data.getPatBedCardStatus() == null || data.getCardStatus() == null || data.getPersonClass() == null) {
            return data;
        }
        if (PatBedCardStatusEnum.OUT_HOSP.equals(data.getPatBedCardStatus())
                || PatBedCardStatusEnum.TRANSFER.equals(data.getPatBedCardStatus())) {
            data.setBedStatus(data.getOtherBedStatus());
        }
        if (data.getBedStatus() == null) {
            return data;
        }

        DesignerModelView dbModelView = data.queryOne();
        if (dbModelView == null) {
            data.setId(0L);
            data.setTemplateJson("");
            return data;
        }

        return dbModelView;
    }

    @Action.Advanced(name = FunctionConstants.create, managed = true, invisible = ExpConstants.idValueExist)
    @Action(displayName = "保存", summary = "创建", bindingType = ViewTypeEnum.FORM)
    @Function(name = FunctionConstants.create)
    @Function.fun(FunctionConstants.create)
    public DesignerModelView create(DesignerModelView data) {
        if (PatBedCardStatusEnum.OUT_HOSP.equals(data.getPatBedCardStatus())
                || PatBedCardStatusEnum.TRANSFER.equals(data.getPatBedCardStatus())) {
            data.setBedStatus(data.getOtherBedStatus());
        }
        data.construct();
        data.create();
        return data;
    }

    @Function.Advanced(type = FunctionTypeEnum.UPDATE)
    @Action.Advanced(name = FunctionConstants.update, managed = true, invisible = ExpConstants.idValueNotExist)
    @Action(displayName = "更新", summary = "修改", bindingType = ViewTypeEnum.FORM)
    @Function(name = FunctionConstants.update)
    @Function.fun(FunctionConstants.update)
    public DesignerModelView update(DesignerModelView data) {
        if (PatBedCardStatusEnum.OUT_HOSP.equals(data.getPatBedCardStatus())
                || PatBedCardStatusEnum.TRANSFER.equals(data.getPatBedCardStatus())) {
            data.setBedStatus(data.getOtherBedStatus());
        }
        data.updateById();
        return data;
    }

    @Function.Advanced(type= FunctionTypeEnum.QUERY)
    @Function.fun(FunctionConstants.queryByEntity)
    @Function(openLevel = {FunctionOpenEnum.API})
    public DesignerModelView queryOne(DesignerModelView query){
        query = query.queryById();
        if (PatBedCardStatusEnum.OUT_HOSP.equals(query.getPatBedCardStatus())
                || PatBedCardStatusEnum.TRANSFER.equals(query.getPatBedCardStatus())) {
            query.setBedStatus(query.getOtherBedStatus());
        }
        return query;
    }

}
